package cn.lg.soar.system.biz.modules.general.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.util.AssertUtil;
import cn.lg.soar.common.util.data.DataUtil;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.model.FileDigestVO;
import cn.lg.soar.system.api.model.FileVO;
import cn.lg.soar.system.biz.config.SystemProps;
import cn.lg.soar.system.biz.filetemplate.model.PartUploadEndVO;
import cn.lg.soar.system.biz.filetemplate.model.PartUploadStartVO;
import cn.lg.soar.system.biz.filetemplate.model.PartUploadUrlInfoVO;
import cn.lg.soar.system.biz.modules.general.entity.FileMetadata;
import cn.lg.soar.system.biz.modules.general.model.UploadConfigVO;
import cn.lg.soar.system.biz.modules.general.service.FileUploadService;
import cn.lg.soar.system.biz.modules.general.service.IFileMetadataService;
import cn.lg.soar.system.biz.util.FileAccessTokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.List;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Api(tags = "文件上传控制")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/file")
public class FileUploadController {

    @Autowired
    private IFileMetadataService fileMetadataService;
    @Autowired
    private FileUploadService fileUploadService;
    @Autowired
    private SystemProps props;

    @ApiOperation("秒传")
    @PostMapping(path = "fastUpload", name = "秒传")
    public HttpResult<List<FileVO>> fastUpload(@ApiParam @RequestBody Collection<FileDigestVO> vos) {
        List<FileMetadata> metadataList = fileUploadService.fastUpload(vos);
        return HttpResult.data(fileUploadService.toVos(metadataList));
    }

    @ApiOperation("上传")
    @PostMapping(path = "upload", name = "上传")
    public HttpResult<List<FileVO>> upload(@ApiParam @RequestParam("files") MultipartFile[] files, @ApiParam String directory) {
        if (DataUtil.isValuable(directory)) {
            List<FileMetadata> metadataList = fileUploadService.upload(directory, files);
            return HttpResult.data(fileUploadService.toVos(metadataList));
        }
        List<FileMetadata> metadataList = fileUploadService.upload(files);
        return HttpResult.data(fileUploadService.toVos(metadataList));
    }

    @ApiOperation("开始分片上传")
    @GetMapping(path = "startMultipartUpload", name = "开始分片上传")
    public HttpResult<PartUploadUrlInfoVO> startMultipartUpload(@ApiParam PartUploadStartVO vo, @ApiParam String directory) {
        if (DataUtil.isValuable(directory)) {
            PartUploadUrlInfoVO partUploadUrlInfoVO = fileUploadService.startMultipartUpload(directory, vo);
            return HttpResult.data(partUploadUrlInfoVO);
        }
        PartUploadUrlInfoVO partUploadUrlInfoVO = fileUploadService.startMultipartUpload(vo);
        return HttpResult.data(partUploadUrlInfoVO);
    }

    @ApiOperation("结束分片上传")
    @GetMapping(path = "endMultipartUpload", name = "结束分片上传")
    public HttpResult<FileVO> endMultipartUpload(@ApiParam PartUploadEndVO vo) {
        return HttpResult.data(fileUploadService.endMultipartUpload(vo));
    }


    @ApiOperation("获取临时访问路径")
    @GetMapping("getTempUrl")
    public HttpResult<String> getTempUrl(@ApiParam @RequestParam long fileId, @ApiParam @RequestParam long expire){
        FileMetadata fileMetadata = fileMetadataService.getById(fileId);
        AssertUtil.notNull(fileMetadata, "文件不存在");
        String anonUrl = FileAccessTokenUtils.createTempUrl(
                fileMetadata.getStorageId(),
                fileMetadata.getDirectory(),
                fileMetadata.filename(),
                expire
        );
        return HttpResult.data(anonUrl);
    }

    @ApiOperation("获取匿名访问路径")
    @GetMapping("getAnonUrl")
    public HttpResult<String> getAnonUrl(@ApiParam @RequestParam long fileId){
        FileMetadata fileMetadata = fileMetadataService.getById(fileId);
        AssertUtil.notNull(fileMetadata, "文件不存在");
        String anonUrl = FileAccessTokenUtils.createAnonUrl(
                fileMetadata.getStorageId(),
                fileMetadata.getDirectory(),
                fileMetadata.filename()
        );
        return HttpResult.data(anonUrl);
    }

    @ApiOperation("获取配置")
    @GetMapping(path = "config", name = "获取配置")
    public HttpResult<UploadConfigVO> config() {
        UploadConfigVO configVo = new UploadConfigVO();
        configVo.setBlacklist(props.getFileUploadBlacklist());
        configVo.setWhitelist(props.getFileUploadWhitelist());
        return HttpResult.data(configVo);
    }

}
